package com.eva.admin.biz.system.settings;

import com.eva.admin.biz.system.settings.dto.SaveSystemSettingDTO;
import com.eva.dao.system.model.SystemSettings;
import com.eva.service.system.SystemSettingsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SystemSettingsBiz {

    @Resource
    private SystemSettingsService systemSettingsService;


    /**
     * 根据设置编码查询
     *
     * @param code 设置编码
     * @return SystemSettings
     */
    public String findValueByCode(String code) {
        SystemSettings settings = systemSettingsService.findByCode(code);
        if (settings == null) {
            return null;
        }
        return settings.getValue();
    }

    /**
     * 保存设置
     *
     * @param dto 保存参数
     */
    public void save (SaveSystemSettingDTO dto) {
        // 根据编码查询
        SystemSettings settings = systemSettingsService.findByCode(dto.getCode());
        // 如果不存在设置项，则新增
        if (settings == null) {
            systemSettingsService.create(SystemSettings.builder()
                    .code(dto.getCode())
                    .name(dto.getName())
                    .value(dto.getValue())
                    .build());
            return;
        }
        // 修改设置项
        systemSettingsService.updateById(SystemSettings.builder()
                .id(settings.getId())
                .name(dto.getName())
                .value(dto.getValue())
                .build());
    }
}
